@echo off & setlocal

GOTO :init

:header
    echo %DOCENTRICAX_NAME% v%DOCENTRICAX_VERSION%
    echo Docentric AX SSRS Replicas model installation and upgrade tool.
	echo.
	echo ** Detected D365FO Paths and tools	
	echo   D365FO AOS Path: %DOCENTRICAX_AOSPath%
	echo   D365FO AOS ModelUtil Tool: %DOCENTRICAX_AOSTOOL_MODELUTIL%
	echo.
	echo ** Detected and installed Docentric AX models
	echo   %DOCENTRICAX_MODEL_MAIN_MODELNAME%=%DOCENTRICAX_MODEL_MAIN_PATH_EXISTS% (Version:%DOCENTRICAX_MODEL_MAIN_VERSION%; Path=%DOCENTRICAX_MODEL_MAIN_PATH%)
	echo   %DOCENTRICAX_MODEL_EXTENSION_MODELNAME%=%DOCENTRICAX_MODEL_EXTENSION_PATH_EXISTS% (Version:%DOCENTRICAX_MODEL_EXTENSION_VERSION%; Path=%DOCENTRICAX_MODEL_EXTENSION_PATH%)
	echo   %DOCENTRICAX_MODEL_EMAILS_MODELNAME%=%DOCENTRICAX_MODEL_EMAILS_PATH_EXISTS% (Version:%DOCENTRICAX_MODEL_EMAILS_VERSION%; Path=%DOCENTRICAX_MODEL_EMAILS_PATH%)
	echo   %DOCENTRICAX_MODEL_SSRSREPLICAS_MODELNAME%=%DOCENTRICAX_MODEL_SSRSREPLICAS_PATH_EXISTS% (Version:%DOCENTRICAX_MODEL_SSRSREPLICAS_VERSION%; Path=%DOCENTRICAX_MODEL_SSRSREPLICAS_PATH%)
	echo   %DOCENTRICAX_MODEL_CBDREPLICAS_MODELNAME%=%DOCENTRICAX_MODEL_CBDREPLICAS_PATH_EXISTS% (Version:%DOCENTRICAX_MODEL_CBDREPLICAS_VERSION%; Path=%DOCENTRICAX_MODEL_CBDREPLICAS_PATH%)
	echo.
    goto :eof

:usage
    echo USAGE:
    echo   %DOCENTRICAX__BAT_NAME% [flags]
    echo.
	echo.  ** NOTE: Please run the tool as administrator
	echo.
	echo.  --^> Available flags
	echo.  /u, --uninstall              uninstall all the core models
	echo.  /?, --help                   shows this help
    echo.  /v, --version                shows the version
	echo.
    goto :eof

:version
    if "%~1"=="full" call :header & goto :eof
    echo %DOCENTRICAX_VERSION%
    goto :eof

:missing_aos
    call :header
    call :usage
    echo.
    echo *******************************************
    echo ****    MISSING "D365FO AOS FOLDER"    ****
    echo *******************************************
    echo.
    goto :eof
:missing_tools
    call :header
    call :usage
    echo.
    echo *******************************************
    echo **** MISSING "REQUIRED D365FO TOOLS"   ****
    echo *******************************************
    echo.
    goto :eof

:elevated_rights
	call :header
	call :usage
    echo.
    echo *********************************************************
    echo ****            ELEVATED RIGHTS REQUIRED             ****
	echo ****      PLEASE RUN THE TOOL AS ADMINISTRATOR.      ****
    echo *********************************************************
    echo.
	goto :eof

:missing_coremodels
    echo.
    echo *********************************************************
    echo ****         MISSING DOCENTRIC AX CORE MODELS        ****
	echo **** PLEASE INSTALL CORE MODELS BEFORE YOU CONTINUE. ****
    echo *********************************************************
    echo.
	goto :eof

:init
	set "DOCENTRICAX_NAME=%~n0"
    set "DOCENTRICAX_VERSION=1.0"
    set "DOCENTRICAX_YEAR=2021"

    set "DOCENTRICAX__BAT_FILE=%~0"
    set "DOCENTRICAX__BAT_PATH=%~dp0"
    set "DOCENTRICAX__BAT_NAME=%~nx0"

    set "DOCENTRICAX_OptHelp="
    set "DOCENTRICAX_OptVersion="
    set "DOCENTRICAX_OptVerbose=no"
	set "DOCENTRICAX_OptQuiet=no"

	REM Find AOS Service path
	set "DOCENTRICAX_AOSPath=[path not found]"
	set "DOCENTRICAX_AOSWebRootPath=[path not found]"	
	
	if exist "C:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=C:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=C:\AosService\WebRoot"
	if exist "D:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=D:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=D:\AosService\WebRoot"
	if exist "E:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=E:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=E:\AosService\WebRoot"
	if exist "F:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=F:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=F:\AosService\WebRoot"
	if exist "G:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=G:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=G:\AosService\WebRoot"
	if exist "H:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=H:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=H:\AosService\WebRoot"
	if exist "I:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=I:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=I:\AosService\WebRoot"
	if exist "J:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=J:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=J:\AosService\WebRoot"
	if exist "K:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=K:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=K:\AosService\WebRoot"
	if exist "M:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=M:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=L:\AosService\WebRoot"
	if exist "N:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=N:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=M:\AosService\WebRoot"
	if exist "O:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=O:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=N:\AosService\WebRoot"
	if exist "P:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=P:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=O:\AosService\WebRoot"
	if exist "Q:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=Q:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=P:\AosService\WebRoot"
	if exist "R:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=R:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=R:\AosService\WebRoot"
	if exist "S:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=S:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=S:\AosService\WebRoot"
	if exist "T:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=T:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=T:\AosService\WebRoot"
	if exist "U:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=U:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=U:\AosService\WebRoot"
	if exist "V:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=V:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=V:\AosService\WebRoot"
	if exist "W:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=W:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=W:\AosService\WebRoot"
	if exist "X:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=X:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=X:\AosService\WebRoot"
	if exist "Y:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=Y:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=Y:\AosService\WebRoot"
	if exist "Z:\AosService\PackagesLocalDirectory\Bin\xppbp.exe" set "DOCENTRICAX_AOSPath=Z:\AosService\PackagesLocalDirectory" & set "DOCENTRICAX_AOSWebRootPath=Z:\AosService\WebRoot"
	
	REM Init AOS Tools
	set "DOCENTRICAX_AOSTOOL_MODELUTIL=[tool is missing]"
	set "DOCENTRICAX_AOSTOOL_XPPC=[tool is missing]"
	set "DOCENTRICAX_AOSTOOL_LABELC=[tool is missing]"
	set "DOCENTRICAX_AOSTOOL_ReportsC=[tool is missing]"
	set "DOCENTRICAX_AOSTOOL_DBSyncEngine=[tool is missing]"

	REM Check if AOS path was found
    if not defined DOCENTRICAX_AOSPath call :missing_aos & goto :end
	if not exist "%DOCENTRICAX_AOSPath%" call :missing_aos & goto :end

	REM Set all tools
	set "DOCENTRICAX_AOSTOOL_MODELUTIL=%DOCENTRICAX_AOSPath%\Bin\modelutil.exe"

	REM Check if all tools exist
	if not defined DOCENTRICAX_AOSTOOL_MODELUTIL call :missing_tools & goto :end
	if not exist "%DOCENTRICAX_AOSTOOL_MODELUTIL%" call :missing_tools & goto :end

	REM Set Docentric modules
	SET "DOCENTRICAX_MODEL_MAIN_MODULENAME=DocentricAX"
	SET "DOCENTRICAX_MODEL_MAIN_MODELNAME=Docentric AX"
	SET "DOCENTRICAX_MODEL_MAIN_VERSION=Unknown"
	SET "DOCENTRICAX_MODEL_MAIN_PATH=%DOCENTRICAX_AOSPath%\%DOCENTRICAX_MODEL_MAIN_MODULENAME%"
	SET "DOCENTRICAX_MODEL_MAIN_VERSION_DESCRIPTOR_FILE=%DOCENTRICAX_MODEL_MAIN_PATH%\Descriptor\%DOCENTRICAX_MODEL_MAIN_MODELNAME%.xml"
	SET "DOCENTRICAX_MODEL_MAIN_PATH_EXISTS=Yes"
	if not exist "%DOCENTRICAX_MODEL_MAIN_PATH%" set "DOCENTRICAX_MODEL_MAIN_PATH_EXISTS=No" & goto skip_read_version_model_main
	if not exist "%DOCENTRICAX_MODEL_MAIN_VERSION_DESCRIPTOR_FILE%" goto skip_read_version_model_main
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionMajor>" ^< "%DOCENTRICAX_MODEL_MAIN_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionMajor=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionMinor>" ^< "%DOCENTRICAX_MODEL_MAIN_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionMinor=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionBuild>" ^< "%DOCENTRICAX_MODEL_MAIN_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionBuild=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionRevision>" ^< "%DOCENTRICAX_MODEL_MAIN_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionRevision=%%~a
	set "DOCENTRICAX_MODEL_MAIN_VERSION=%DocentricAX_model_versionMajor%.%DocentricAX_model_versionMinor%.%DocentricAX_model_versionBuild%.%DocentricAX_model_versionRevision%"
	:skip_read_version_model_main
	
	SET "DOCENTRICAX_MODEL_EXTENSION_MODULENAME=DocentricAXExtension"
	SET "DOCENTRICAX_MODEL_EXTENSION_MODELNAME=Docentric AX Extension"
	SET "DOCENTRICAX_MODEL_EXTENSION_VERSION=Unknown"
	SET "DOCENTRICAX_MODEL_EXTENSION_PATH=%DOCENTRICAX_AOSPath%\%DOCENTRICAX_MODEL_EXTENSION_MODULENAME%"
	SET "DOCENTRICAX_MODEL_EXTENSION_VERSION_DESCRIPTOR_FILE=%DOCENTRICAX_MODEL_EXTENSION_PATH%\Descriptor\%DOCENTRICAX_MODEL_EXTENSION_MODELNAME%.xml"
	SET "DOCENTRICAX_MODEL_EXTENSION_PATH_EXISTS=Yes"
	if not exist "%DOCENTRICAX_MODEL_EXTENSION_PATH%" set "DOCENTRICAX_MODEL_EXTENSION_PATH_EXISTS=No" & goto skip_read_version_model_extension
	if not exist "%DOCENTRICAX_MODEL_EXTENSION_VERSION_DESCRIPTOR_FILE%" goto skip_read_version_model_extension
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionMajor>" ^< "%DOCENTRICAX_MODEL_EXTENSION_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionMajor=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionMinor>" ^< "%DOCENTRICAX_MODEL_EXTENSION_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionMinor=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionBuild>" ^< "%DOCENTRICAX_MODEL_EXTENSION_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionBuild=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionRevision>" ^< "%DOCENTRICAX_MODEL_EXTENSION_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionRevision=%%~a
	set "DOCENTRICAX_MODEL_EXTENSION_VERSION=%DocentricAX_model_versionMajor%.%DocentricAX_model_versionMinor%.%DocentricAX_model_versionBuild%.%DocentricAX_model_versionRevision%"
	:skip_read_version_model_extension

	SET "DOCENTRICAX_MODEL_EMAILS_MODULENAME=DocentricAXEmails"
	SET "DOCENTRICAX_MODEL_EMAILS_MODELNAME=Docentric AX Emails"
	SET "DOCENTRICAX_MODEL_EMAILS_VERSION=Unknown"
	SET "DOCENTRICAX_MODEL_EMAILS_PATH=%DOCENTRICAX_AOSPath%\%DOCENTRICAX_MODEL_EMAILS_MODULENAME%"
	SET "DOCENTRICAX_MODEL_EMAILS_VERSION_DESCRIPTOR_FILE=%DOCENTRICAX_MODEL_EMAILS_PATH%\Descriptor\%DOCENTRICAX_MODEL_EMAILS_MODELNAME%.xml"
	SET "DOCENTRICAX_MODEL_EMAILS_PATH_EXISTS=Yes"
	if not exist "%DOCENTRICAX_MODEL_EMAILS_PATH%" SET "DOCENTRICAX_MODEL_EMAILS_PATH_EXISTS=No" & goto skip_read_version_model_emails
	if not exist "%DOCENTRICAX_MODEL_EMAILS_VERSION_DESCRIPTOR_FILE%" goto skip_read_version_model_emails
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionMajor>" ^< "%DOCENTRICAX_MODEL_EMAILS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionMajor=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionMinor>" ^< "%DOCENTRICAX_MODEL_EMAILS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionMinor=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionBuild>" ^< "%DOCENTRICAX_MODEL_EMAILS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionBuild=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionRevision>" ^< "%DOCENTRICAX_MODEL_EMAILS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionRevision=%%~a
	set "DOCENTRICAX_MODEL_EMAILS_VERSION=%DocentricAX_model_versionMajor%.%DocentricAX_model_versionMinor%.%DocentricAX_model_versionBuild%.%DocentricAX_model_versionRevision%"
	:skip_read_version_model_emails
	
	SET "DOCENTRICAX_MODEL_SSRSREPLICAS_MODULENAME=DocentricAXSSRSReplicas"
	SET "DOCENTRICAX_MODEL_SSRSREPLICAS_MODELNAME=Docentric AX SSRS Replicas"
	SET "DOCENTRICAX_MODEL_SSRSREPLICAS_VERSION=Unknown"
	SET "DOCENTRICAX_MODEL_SSRSREPLICAS_PATH=%DOCENTRICAX_AOSPath%\%DOCENTRICAX_MODEL_SSRSREPLICAS_MODULENAME%"
	SET "DOCENTRICAX_MODEL_SSRSREPLICAS_VERSION_DESCRIPTOR_FILE=%DOCENTRICAX_MODEL_SSRSREPLICAS_PATH%\Descriptor\%DOCENTRICAX_MODEL_SSRSREPLICAS_MODELNAME%.xml"
	SET "DOCENTRICAX_MODEL_SSRSREPLICAS_PATH_EXISTS=Yes"
	if not exist "%DOCENTRICAX_MODEL_SSRSREPLICAS_PATH%" SET "DOCENTRICAX_MODEL_SSRSREPLICAS_PATH_EXISTS=No" & goto skip_read_version_model_ssrsreplicas
	if not exist "%DOCENTRICAX_MODEL_SSRSREPLICAS_VERSION_DESCRIPTOR_FILE%" goto skip_read_version_model_ssrsreplicas
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionMajor>" ^< "%DOCENTRICAX_MODEL_SSRSREPLICAS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionMajor=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionMinor>" ^< "%DOCENTRICAX_MODEL_SSRSREPLICAS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionMinor=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionBuild>" ^< "%DOCENTRICAX_MODEL_SSRSREPLICAS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionBuild=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionRevision>" ^< "%DOCENTRICAX_MODEL_SSRSREPLICAS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionRevision=%%~a
	set "DOCENTRICAX_MODEL_SSRSREPLICAS_VERSION=%DocentricAX_model_versionMajor%.%DocentricAX_model_versionMinor%.%DocentricAX_model_versionBuild%.%DocentricAX_model_versionRevision%"
	:skip_read_version_model_ssrsreplicas
	
	SET "DOCENTRICAX_MODEL_CBDREPLICAS_MODULENAME=DocentricAXCBDReplicas"
	SET "DOCENTRICAX_MODEL_CBDREPLICAS_MODELNAME=Docentric AX CBD Replicas"
	SET "DOCENTRICAX_MODEL_CBDREPLICAS_VERSION=Unknown"
	SET "DOCENTRICAX_MODEL_CBDREPLICAS_PATH=%DOCENTRICAX_AOSPath%\%DOCENTRICAX_MODEL_CBDREPLICAS_MODULENAME%"
	SET "DOCENTRICAX_MODEL_CBDREPLICAS_VERSION_DESCRIPTOR_FILE=%DOCENTRICAX_MODEL_CBDREPLICAS_PATH%\Descriptor\%DOCENTRICAX_MODEL_CBDREPLICAS_MODELNAME%.xml"
	SET "DOCENTRICAX_MODEL_CBDREPLICAS_PATH_EXISTS=Yes"
	if not exist "%DOCENTRICAX_MODEL_CBDREPLICAS_PATH%" SET "DOCENTRICAX_MODEL_CBDREPLICAS_PATH_EXISTS=No" & goto skip_read_version_model_cbdreplicas
	if not exist "%DOCENTRICAX_MODEL_CBDREPLICAS_VERSION_DESCRIPTOR_FILE%" goto skip_read_version_model_cbdreplicas
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionMajor>" ^< "%DOCENTRICAX_MODEL_CBDREPLICAS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionMajor=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionMinor>" ^< "%DOCENTRICAX_MODEL_CBDREPLICAS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionMinor=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionBuild>" ^< "%DOCENTRICAX_MODEL_CBDREPLICAS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionBuild=%%~a
	for /f "tokens=3 delims=<>" %%a in ('find /i "<VersionRevision>" ^< "%DOCENTRICAX_MODEL_CBDREPLICAS_VERSION_DESCRIPTOR_FILE%"') do set DocentricAX_model_versionRevision=%%~a
	set "DOCENTRICAX_MODEL_CBDREPLICAS_VERSION=%DocentricAX_model_versionMajor%.%DocentricAX_model_versionMinor%.%DocentricAX_model_versionBuild%.%DocentricAX_model_versionRevision%"
	:skip_read_version_model_cbdreplicas
	
	REM Detect if AOS applications are running (IIS, IISExpress, Batch.exe)
	set "DOCENTRICAX_AOSWEB_RUNNING=No"
	set "DOCENTRICAX_AOSBATCH_RUNNING=No"
	tasklist /fi "IMAGENAME eq w3wp.exe" | find /i "w3wp.exe">NUL
	if %ERRORLEVEL% equ 0 set "DOCENTRICAX_AOSWEB_RUNNING=Yes"
	tasklist /fi "IMAGENAME eq iisexpress.exe" | find /i "iisexpress.exe">NUL
	if %ERRORLEVEL% equ 0 set "DOCENTRICAX_AOSWEB_RUNNING=Yes"
	REM if none of the exe files are running maybe IIS is waiting for the first command
	for /f "tokens=4" %%a in ('sc query W3SVC ^| find "STATE"') do if /i "%%~a" equ "RUNNING" set "DOCENTRICAX_AOSWEB_RUNNING=Yes"
	tasklist /fi "IMAGENAME eq batch.exe" | find /i "batch.exe">NUL
	if %ERRORLEVEL% equ 0 set "DOCENTRICAX_AOSBATCH_RUNNING=Yes"
	
:parse
    if "%~1"=="" goto :validate

	if /i "%~1"=="/u"         set "DOCENTRICAX_UNINSTALL=yes"  & shift & goto :parse
    if /i "%~1"=="-u"         set "DOCENTRICAX_UNINSTALL=yes"  & shift & goto :parse
    if /i "%~1"=="--uninstall"  set "DOCENTRICAX_UNINSTALL=yes"  & shift & goto :parse

    if /i "%~1"=="/?"         call :header & call :usage "%~2" & goto :end
    if /i "%~1"=="-?"         call :header & call :usage "%~2" & goto :end
    if /i "%~1"=="--help"     call :header & call :usage "%~2" & goto :end

    if /i "%~1"=="/v"         call :version      & goto :end
    if /i "%~1"=="-v"         call :version      & goto :end
    if /i "%~1"=="--version"  call :version full & goto :end

    shift
    goto :parse

:validate
	
	REM Check for elevated rights
	net.exe session 1>NUL 2>NUL || (call :elevated_rights & goto :end)

	goto main

:stop_aos_applications
	REM To recycle IIS web apps or IIS Express web apps rename the web.config to other name and then rename it back
	echo Stopping AOS applications	
	echo.
	if /i "%DOCENTRICAX_AOSWEB_RUNNING%" equ "Yes" (	
		echo   Stopping Internet services...
		iisreset /stop
		taskkill /IM w3wp.exe /F 1>NUL 2>NUL
		
		echo   Stopping AOS Web applications...
		move /Y "%DOCENTRICAX_AOSWebRootPath%\web.config" "%DOCENTRICAX_AOSWebRootPath%\___web.config">NUL
		move /Y "%DOCENTRICAX_AOSWebRootPath%\___web.config" "%DOCENTRICAX_AOSWebRootPath%\web.config">NUL
	) else echo   AOS Web not running
	
	if /i "%DOCENTRICAX_AOSBATCH_RUNNING%" equ "Yes" (
		echo   Stopping AOS Batch service...
		call net stop DynamicsAxBatch
		taskkill /IM batch.exe /F 1>NUL 2>NUL
	) else echo   AOS Batch service not running
	
	echo   Terminating Visual Studio related assembly locks
	taskkill /IM msvsmon.exe /F 1>NUL 2>NUL
	echo.
	TIMEOUT /T 2 /NOBREAK 1>NUL 2>NUL
	
	goto :eof

:start_aos_applications
	set "_aos_apps_start_needed="
	if /i "%DOCENTRICAX_AOSWEB_RUNNING%" equ "Yes" set "_aos_apps_start_needed=1"
	if /i "%DOCENTRICAX_AOSBATCH_RUNNING%" equ "Yes" set "_aos_apps_start_needed=1"
	if "%_aos_apps_start_needed%" neq "1" goto :eof
	
	echo.
	echo Starting AOS applications

	if /i "%DOCENTRICAX_AOSWEB_RUNNING%" equ "Yes" (
		REM JUST IN CASE SOMEONE TEMPERED WITH IIS WE FIRST STOP IT AND THEN WE RUN IT AGAIN
		iisreset /stop>NUL
		taskkill /IM w3wp.exe /F 1>NUL 2>NUL
		echo.		
		echo   Starting AOS Web applications and Internet services...
		iisreset /start
	)

	if /i "%DOCENTRICAX_AOSBATCH_RUNNING%" equ "Yes" (
		echo.
		echo   Starting AOS Batch service...
		call net start DynamicsAxBatch
	)
	
	goto :eof

:main
	call :header

	if /i "%DOCENTRICAX_UNINSTALL%" EQU "Yes" goto uninstall
	
	REM DETECT present model files
	set "DOCENTRICAX_CBDREPLICAS_MODEL_FILE=%DOCENTRICAX__BAT_PATH%Docentric AX CBD Replicas.axmodel"
	
	set /p "DOCENTRICAX_CBDREPLICAS_MODEL_FILE=Enter the Docentric AX CBD Replicas model with full path or press ENTER to confirm [%DOCENTRICAX_CBDREPLICAS_MODEL_FILE%]: "
	
	if not exist "%DOCENTRICAX_CBDREPLICAS_MODEL_FILE%" echo "Model file %DOCENTRICAX_CBDREPLICAS_MODEL_FILE% does not exist!" & goto end
	
	if exist "%DOCENTRICAX_MODEL_CBDREPLICAS_VERSION_DESCRIPTOR_FILE%" echo. & echo NOTE: ANY CUSTOMIZATIONS WILL BE DELETED. IF YOU HAVE CHANGED ANYTHING MAKE SURE YOU CREATED A BACKUP BEFORE YOU CONTINUE!
	set /p "DOCENTRICAX_PROCEEDWITHINSTALL=Are you sure you want to install or upgrade Docentric AX CBD Replicas model? Enter YES to continue: "
	if /i "%DOCENTRICAX_PROCEEDWITHINSTALL%" NEQ "Yes" set errorlevel=0 & goto :end

	if not exist "%DOCENTRICAX_MODEL_MAIN_VERSION_DESCRIPTOR_FILE%" goto :missing_coremodels
	if not exist "%DOCENTRICAX_MODEL_EXTENSION_VERSION_DESCRIPTOR_FILE%" goto :missing_coremodels
	if not exist "%DOCENTRICAX_MODEL_EMAILS_VERSION_DESCRIPTOR_FILE%" goto :missing_coremodels
	if not exist "%DOCENTRICAX_MODEL_SSRSREPLICAS_VERSION_DESCRIPTOR_FILE%" goto :missing_coremodels

	call :stop_aos_applications
	if %ERRORLEVEL% neq 0 echo AOS applications can not be stopped. Terminating...& goto end

	echo Installing model...
	echo.
	if exist "%DOCENTRICAX_MODEL_CBDREPLICAS_VERSION_DESCRIPTOR_FILE%" (
		echo   Upgrading model: %DOCENTRICAX_MODEL_CBDREPLICAS_MODELNAME%
		"%DOCENTRICAX_AOSTOOL_MODELUTIL%" -replace -force -metadatastorepath="%DOCENTRICAX_AOSPath%" -modelname="%DOCENTRICAX_MODEL_CBDREPLICAS_MODELNAME%" -file="%DOCENTRICAX_CBDREPLICAS_MODEL_FILE%"
		if %errorlevel% NEQ 0 call :start_aos_applications & goto end
	) else (
		echo   Installing model: %DOCENTRICAX_MODEL_CBDREPLICAS_MODELNAME%
		"%DOCENTRICAX_AOSTOOL_MODELUTIL%" -import -metadatastorepath="%DOCENTRICAX_AOSPath%" -modelname="%DOCENTRICAX_MODEL_CBDREPLICAS_MODELNAME%" -file="%DOCENTRICAX_CBDREPLICAS_MODEL_FILE%"
		if %errorlevel% NEQ 0 call :start_aos_applications & goto end
	)
	
	call :start_aos_applications
	
	goto end
	
:uninstall
	
	echo.
	echo NOTE: ANY CUSTOMIZATIONS WILL BE DELETED. IF YOU HAVE CHANGED ANYTHING MAKE SURE YOU CREATED A BACKUP BEFORE YOU CONTINUE!
	set /p "DOCENTRICAX_PROCEEDWITHUNINSTALL=Are you sure you want to uninstall? Enter YES to continue: "
	if /i "%DOCENTRICAX_PROCEEDWITHUNINSTALL%" NEQ "Yes" set errorlevel=0 & goto :end	

	call :stop_aos_applications
	if %ERRORLEVEL% neq 0 echo AOS applications can not be stopped. Terminating...& goto end
	
	echo Uninstalling model...
	echo   Removing model: %DOCENTRICAX_MODEL_CBDREPLICAS_MODELNAME%
	"%DOCENTRICAX_AOSTOOL_MODELUTIL%" -delete -force -metadatastorepath="%DOCENTRICAX_AOSPath%" -modelname="%DOCENTRICAX_MODEL_CBDREPLICAS_MODELNAME%"
	echo.
	
	echo Removing empty folders...
    if not exist "%DOCENTRICAX_MODEL_CBDREPLICAS_VERSION_DESCRIPTOR_FILE%" rmdir /Q /S "%DOCENTRICAX_MODEL_CBDREPLICAS_PATH%"
	echo Done
	
	call :start_aos_applications
	
	goto end
	
:end
:exit
	endlocal

	IF %ERRORLEVEL% NEQ 0 TIMEOUT 10
	
	@ECHO ON